<template>
	<view class="">
		<cc-couponCentreList :subscript="0" :colors="colors" :couponList="dynamicList" @itemClick="jumpNext"></cc-couponCentreList>
		</view>
	</view>
</template>

<script>
	// 导入cc-couponCentreList组件
	import CCCouponCentreList from '@/uni_modules/cc-couponList/components/cc-couponList/cc-couponCentreList.vue';
	export default {
		components: {
		    'cc-couponCentreList': CCCouponCentreList
		},
		data() {
			return {
				colors: '#e54d42',
				dynamicList: [], // 优惠卷列表
			};
		},
		methods: {
			
			jumpNext(item) {
				// 点击领取优惠券，判断优惠券剩余数量
				if (item.num > 0) {
					uni.showModal({
						title: '领取优惠券',
						content: '确定领取' + '"' + item.name + '"',
						success: function (res) {
						if (res.confirm) {
						  // 用户点击了确定，可以在这里处理领取优惠券的逻辑
						  const token = uni.getStorageSync('Token')
						  const uid = uni.getStorageSync('UserInfo').id
						  uni.request({
						  	url:'http://localhost/dev-api/system/userCoupon',
						  	method: 'POST',
							data: {
								uId: uid,
								cId: item.id,	
							},
						  	header: {
						  		'Authorization': 'Bearer ' + token,
						  	},
						  	success: (res) => {
						  		if (res.statusCode === 200) {
									if (res.data.code === 500) {
										uni.showToast({
											title: '优惠券已领取',
											icon: 'none'
										});
									} else {
										uni.showToast({
											title: '优惠券领取成功',
											icon: 'none'
										});
									}
						  		} else {
						  			uni.showToast({
						  				title: '优惠券领取失败',
						  				icon: 'none'
						  			});
						  			
						  		}
						  	},
						  	fail: (err) => {
						  		console.log("请求失败");
						  	},
						  });
						} else if (res.cancel) {
						  // 用户点击了取消
						}
					  }
					})
				} else {
					uni.showToast({
						title: '优惠券已领完',
						icon: 'none',
						duration: 2000,
					});
				}
			}
			
			// 领取优惠券
		},
		onLoad() {
			// 获取优惠卷信息
			const token = uni.getStorageSync('Token')
			uni.request({
				url:'http://localhost/dev-api/system/coupons/list',
				method: 'GET',
				header: {
					'Authorization': 'Bearer ' + token,
				},
				success: (res) => {
					if (res.statusCode === 200) {
						this.dynamicList = res.data.rows;
						// console.log("res...............................");
						console.log("this.dynamicList",this.dynamicList);
					} else {
						console.log("请求失败");
						
					}
				},
				fail: (err) => {
					console.log("请求失败");
				},
			});
		}
	}
</script>

<style>
	
</style>